﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Tst.Models
{
    public class PlanoTesteRepository
    {
        private TstEntities context;

        public PlanoTesteRepository()
        {
            this.context = new TstEntities();
        }

        ~PlanoTesteRepository()
        {
            this.context.Dispose();
        }

        public void Save()
        {
            this.context.SaveChanges();
        }

        public IList<PlanoTeste> GetAll()
        {
            return this.context.PlanoTeste.ToList();
        }

        public PlanoTeste GetById(int cd_planoTeste)
        {
            return this.context.PlanoTeste.FirstOrDefault(p => p.cd_plano_teste == cd_planoTeste);
        }

        public PlanoTeste GetByDescricao(String ds_descricao)
        {
            return this.context.PlanoTeste.FirstOrDefault(p => p.ds_descricao.Equals(ds_descricao));
        }

        public PlanoTeste Insert(PlanoTeste planoTeste)
        {
            this.context.AddToPlanoTeste(planoTeste);
            return planoTeste;
        }

        public void Update(PlanoTeste planoTeste)
        {
            this.context.AttachUpdated(planoTeste);
        }

        public void Delete(int cd_plano_teste)
        {
            PlanoTeste planoTeste = this.GetById(cd_plano_teste);

            if (planoTeste != null)
            {
                planoTeste.ProjetoReference.Load();
                planoTeste.Projeto = null;
                planoTeste.ModuloReference.Load();
                planoTeste.Modulo = null;                

                this.context.DeleteObject(planoTeste);
            }
        }
    }
}
